package com.atguigu.channel;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * className: SocketChannelDemo
 * description:
 * date: 2023/3/9 0009-15:04
 * <p>
 * project: nio
 * package: com.atguigu.channel
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public class SocketChannelDemo {

    public static void main(String[] args) throws IOException {
        // 创建 SocketChannel
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("www.baidu.com", 80));

        // SocketChannel socketChannel2 = SocketChannel.open();
        // socketChannel2.connect(new InetSocketAddress("www.baidu.com", 80));

        // 设置阻塞和非阻塞
        socketChannel.configureBlocking(false);

        // 读操作
        ByteBuffer buffer = ByteBuffer.allocate(16);
        socketChannel.read(buffer);
        socketChannel.close();
        System.out.println("read over");
    }
}
